class Article < ActiveRecord::Base
	
	acts_as_taggable
	acts_as_ferret :fields => [:title, :content]
	acts_as_enum :article_type, [:product, :article]

	belongs_to :user
	belongs_to :category
	has_many :comments, :as => :commentable, :dependent => :destroy
	has_many :images, :as=>:assetable, :dependent=>:destroy
	
	named_scope :articles, :conditions=>{:article_type=>Article.article_type(:article)}
	named_scope :products, :conditions=>{:article_type=>Article.article_type(:product)}
	
	def is_gmap?
		!self.point_x.blank? && !self.point_y.blank?	
	end
	
	def self.last_update(category_id)
		article = find(:first, :conditions=>"category_id = #{category_id}", :order=>"updated_at DESC")
		article ? article.updated_at : DateTime.now
	end	

	def to_param
		self.id.to_s + "-" + self.slug
	end
	
	def before_save
		self.slug = self.title.parameterize if self.slug.blank?
	end
end
